import plugins from './Plugin.js'
export class Stack {
  constructor() {
    this.stack = []
    this.list = []
    this.plugins = plugins
  }
  push(elem) {
    if (this.stack.length > 0) {
      let topPlugin = this.getTopPlugin()
      if (topPlugin) {
        let flag = topPlugin.contain(elem)
        if (flag == 1) {
        } else if (flag == 2) {
          let newPlugin = this.addPlugin(elem)
          topPlugin.addChild(newPlugin)
        } else if (flag == 3) {
          //这里要结束栈，使用stop
          this.stack.pop()
          this.push(elem)
        }
      }
    } else {
      let plugin = this.addPlugin(elem)
      this.saveTop(plugin)
    }
  }
  findPlugin(elem) {
    let pluginConstructor = this.plugins.find((res) => res.type == elem.type)
    if (pluginConstructor) {
      return new pluginConstructor(elem)
    }
  }
  addPlugin(elem) {
    let plugin = this.findPlugin(elem)
    this.stack.push(plugin)
    return plugin
  }
  getTopPlugin() {
    return this.stack[this.stack.length - 1]
  }
  saveTop(plugin) {
    this.list.push(plugin)
    return this
  }
}
